package com.music.controller;

import com.music.pojo.Record;
import com.music.service.RecordService;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.pojo.Record;
import com.music.service.RecordService;
import com.music.util.Result;

import java.util.List;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordServiceImpl;

    @RequestMapping("/play")
    @ResponseBody
    public Result play(Integer userId,Integer musicId){
        Record record=recordServiceImpl.getRecord(userId,musicId);
        if(record==null){
            record.setUserId(userId);
            record.setMusicId(musicId);
            record.setPlayCount(1);
            recordServiceImpl.insert(record);
        }else {
            record.setPlayCount(record.getPlayCount()+1);
            recordServiceImpl.update(record);
        }
        return Result.success("操作成功");
    }

    /***
     * 新增
     * @param record
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Record record) {
        int result = recordServiceImpl.insert(record);
        return Result.success("添加成功", result);
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id) {
        int result = recordServiceImpl.delete(id);
        return Result.success("删除成功", result);
    }

    /***
     * 修改
     * @param record
     * @param id
     * @return
     */
    @RequestMapping(value="/update")
    @ResponseBody
    public Result update(Integer id, Record record) {
        record.setId(id);
        int result = recordServiceImpl.update(record);
        return Result.success("修改成功");
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Integer id) {
        Record record = recordServiceImpl.getByPrimaryKey(id);
        return Result.success("查询成功", record);
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(Record record, int page, int size) {
        PageInfo<Record> pageInfo = recordServiceImpl.listPageInfo(record, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(Record record, int page, int size) {
        List<Record> list = recordServiceImpl.listPage(record, page, size);
        return Result.success("查询成功", list);
    }
}